package com.jingdianjichi.auth.domain.service.Impl;

import com.alibaba.fastjson.JSON;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jingdianjichi.auth.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.auth.domain.bo.AuthPermissionBO;
import com.jingdianjichi.auth.domain.bo.AuthRoleBO;
import com.jingdianjichi.auth.domain.convert.AuthPermissionBOConvert;
import com.jingdianjichi.auth.domain.convert.AuthRoleBOConvert;
import com.jingdianjichi.auth.domain.redis.RedisUtil;
import com.jingdianjichi.auth.domain.service.AuthPermissionDomainService;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import com.jingdianjichi.auth.infra.bastic.entity.AuthRole;
import com.jingdianjichi.auth.infra.bastic.service.AuthPermissionService;
import com.jingdianjichi.auth.infra.bastic.service.AuthRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.jingdianjichi.auth.infra.bastic.entity.AuthPermission;
import sun.security.krb5.internal.rcache.AuthList;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:AuthRoleDomainServiceImpl
 * Package:com.jingdianjichi.auth.domain.service.Impl
 * Description:
 *
 * @Author fst
 * @Create 2024/10/10 14:58
 * @Version 1.0
 */
@Slf4j
@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService
{

    @Resource
    private  AuthPermissionService authPermissionService;


    @Resource
    private RedisUtil redisUtil;

    private String authPermissionPrefix="auth.permission";


    public  void add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.add.Bo:{}", JSON.toJSONString(authPermissionBO));
        }

        AuthPermission authPermission = AuthPermissionBOConvert.INSTANCE.convertBOTOPermission(authPermissionBO);

        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        authPermissionService.insert(authPermission);
    }

    public  void update(AuthPermissionBO authPermissionBO)
    {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.update.Bo:{}", JSON.toJSONString(authPermissionBO));
        }

        AuthPermission authPermission = AuthPermissionBOConvert.INSTANCE.convertBOTOPermission(authPermissionBO);

        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        authPermissionService.update(authPermission);
    }

    public  void delete(AuthPermissionBO authPermissionBO)
    {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.delete.Bo:{}", JSON.toJSONString(authPermissionBO));
        }

        AuthPermission authPermission = AuthPermissionBOConvert.INSTANCE.convertBOTOPermission(authPermissionBO);

        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        authPermissionService.delete(authPermission);
    }

    public List<String> getPermission(String userName)
    {
        String permissionKey =redisUtil.buildKey(authPermissionPrefix,userName);
        String permissionValue = redisUtil.get(permissionKey);

        if(permissionValue.equals("")) return Collections.emptyList();

        List<AuthPermission> permissionList =new Gson().fromJson(permissionValue,new TypeToken<List<AuthPermission>>(){}.getType());

        List<String> authList = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());

        return authList;
    }
}
